//
//  FindTableViewController.m
//  高仿萝卜书摘
//
//  Created by 谢俊逸 on 2016/11/22.
//  Copyright © 2016年 谢俊逸. All rights reserved.
//

#import "FindTableViewController.h"
#import "ActiveAreaTableViewCell.h"
#import "ThematicCollectionTableViewCell.h"
#import "BookFriendsTableViewCell.h"
#import "RecommendGroupsTableViewCell.h"


#import "FindSearchDisplayController.h"
#import "SDCycleScrollView.h"
#define FindTableViewCell1 @"FindTableViewCell1"
#define FindTableViewCell2 @"FindTableViewCell2"
#define FindTableViewCell3 @"FindTableViewCell3"
#define FindTableViewCell4 @"FindTableViewCell4"

@interface FindTableViewController ()<SDCycleScrollViewDelegate,UISearchResultsUpdating, UISearchBarDelegate,UISearchDisplayDelegate>

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResults;
@property (nonatomic, strong) UISearchBar *searchBar;
@end

@implementation FindTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //[self setupHeaderScrollViewFromInternat];
    [self setupHeaderScrollViewFromLocal];
    [self registerCell];
    //自定义比较麻烦
    //[self setupTitleView];
    //searchController 点击会往上跳 不好用
    //[self setupScarchController];
    [self setupSearchBar];

}
- (void)setupSearchBar{
    //加上 搜索栏
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 35)];//allocate titleView
    UIColor *color =  self.navigationController.navigationBar.barTintColor;
    
    [titleView setBackgroundColor:color];
    
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    
    searchBar.delegate = self;
    searchBar.frame = CGRectMake(0, 0, 300, 35);
    searchBar.backgroundColor = color;
    searchBar.layer.cornerRadius = 18;
    searchBar.layer.masksToBounds = YES;
    [searchBar.layer setBorderWidth:5];
    [searchBar.layer setBorderColor:[UIColor whiteColor].CGColor];  //设置边框为白色
    _searchBar = searchBar;
    searchBar.placeholder = @"搜索作者、书名、书摘内容等";
    [titleView addSubview:searchBar];
    
    //Set to titleView
    [self.navigationItem.titleView sizeToFit];
    self.navigationItem.titleView = titleView;
}
//
//- (void)setupScarchController{
//    // Create a mutable array to contain products for the search results table.
//
//    
//    // The collection view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
//    //UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"CollectionSearchResultsNavController"];
//    UIViewController *viewC = [[UIViewController alloc]init];
//    self.searchController = [[UISearchController alloc] initWithSearchResultsController:viewC];
//    
//    self.searchController.searchResultsUpdater = self;
//    
//    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x+30, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
//    //2016-11-26 19:22:39.710 高仿萝卜书摘[7370:619652] 0.000000 0.000000 375.000000 22.000000
//    //NSLog(@"%f %f %d %f",self.searchController.searchBar.frame.origin.x,self.searchController.searchBar.frame.origin.y,self.searchController.searchBar.frame.size.width,44);
//    
//    
//    self.navigationItem.titleView = self.searchController.searchBar;
//    
//    self.definesPresentationContext = YES;
//}



- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:@"ActiveAreaTableViewCell" bundle:nil]forCellReuseIdentifier:FindTableViewCell1];
    [self.tableView registerNib:[UINib nibWithNibName:@"ThematicCollectionTableViewCell" bundle:nil]forCellReuseIdentifier:FindTableViewCell2];
    [self.tableView registerNib:[UINib nibWithNibName:@"BookFriendsTableViewCell" bundle:nil]forCellReuseIdentifier:FindTableViewCell3];
    [self.tableView registerNib:[UINib nibWithNibName:@"RecommendGroupsTableViewCell" bundle:nil]forCellReuseIdentifier:FindTableViewCell4];
    
}
- (void)setupHeaderScrollViewFromLocal{
    // 情景一：采用本地图片实现
    NSArray *imageNames = @[@"page1.jpg",
                            @"page2.jpg",
                            @"page3.jpg",
                            @"page4.jpg",
                            ];
    CGFloat w = self.view.bounds.size.width;
    // 本地加载 --- 创建不带标题的图片轮播器
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 64, w, 180) shouldInfiniteLoop:YES imageNamesGroup:imageNames];
    cycleScrollView.delegate = self;
    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    self.tableView.tableHeaderView = cycleScrollView;
    cycleScrollView.scrollDirection = UICollectionViewScrollDirectionVertical;

}

- (void)setupHeaderScrollViewFromInternat{

    NSArray *imagesURLStrings = @[
                                 
                                  ];
    CGFloat w = self.view.bounds.size.width;

    
    // 网络加载 --- 创建带标题的图片轮播器
    SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 280, w, 180) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView2.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    self.tableView.tableHeaderView = cycleScrollView2;
    //         --- 模拟加载延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView2.imageURLStringsGroup = imagesURLStrings;
    });
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    

    switch (indexPath.section) {
        case 0:{
            ActiveAreaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FindTableViewCell1];
            cell.indexPath = indexPath;
            return cell;
        }
            break;
        case 1:{
            ThematicCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FindTableViewCell2];
            cell.indexPath = indexPath;
            return cell;
        }
            ;
            break;
        case 2:{
            BookFriendsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FindTableViewCell3];
            cell.indexPath = indexPath;
            return cell;
        }
            
            break;
        case 3:{
            RecommendGroupsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FindTableViewCell4];
            cell.indexPath = indexPath;
            return cell;
        }
            
            break;
            
        default:
        {
            ActiveAreaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FindTableViewCell1];
        }
            break;
    }
    ActiveAreaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FindTableViewCell1];
    cell.indexPath = indexPath;
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            return 200;
            break;
        case 1:
            return 380;
            break;
        case 2:
            return 110;
            break;
        case 3:
            return 110;
            break;
        default:
            return 150;
            break;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
        {
            return @"活动区";
        }
            break;
        case 1:
        {
            return @"专题集";
        }
            break;
        case 2:
        {
            return @"萝卜书友";
        }
            break;
        case 3:
        {
            return @"推荐萝卜坑";
        }
            break;
            
        default:
            break;
    }
    return @"";
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
    
    [self.navigationController pushViewController:[NSClassFromString(@"DemoVCWithXib") new] animated:YES];
}

#pragma mark - UISearchBarDelegate

// Workaround for bug: -updateSearchResultsForSearchController: is not called when scope buttons change
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {

}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"搜索Begin");
    return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    NSLog(@"搜索End");
    return YES;
}
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [_searchBar resignFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [_searchBar resignFirstResponder];
}

- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar{
    [_searchBar resignFirstResponder];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    return YES;
}
#pragma mark - UISearchResultsUpdating

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
}

#pragma touchResignFirstResponder

@end
